package com.ruoyi.system.AnsMsg;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MqXiaoxi {

    @Autowired
    RestHighLevelClient restHighLevelClient;


    @RabbitListener(queues = "queues1")
    public void jiemq(Message message, Channel channel) throws IOException {
        if(message.getBody().length>0 && message.getBody()!=null){
            System.out.println(message.getBody()+"=======================================");
            try {
                IndexRequest roominfo = new IndexRequest("roominfo");
                IndexRequest source = roominfo.source(message.getBody(), XContentType.JSON);
                IndexResponse index = restHighLevelClient.index(roominfo, RequestOptions.DEFAULT);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                //持久化
//                channel.exchangeDeclare("fanoutExchange","fanout",true);
//                channel.queueDeclare("queue1",true,false,false,null);
                channel.basicPublish("fanoutExchange","", MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBody());
            } catch (IOException e) {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            }

        }
    }
}
